package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(String email, String password) {
        User user = userService.findOne(email, password);
        //对返回值user对象进行判断
        if (user!=null){
//            返回成功页面
            return "main";
        }
        return "login";
    }



    @RequestMapping("/list")
    public String list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize",defaultValue = "1") Integer pageSize,
                       Model model){

        List<User> userList = userService.findList(pageNum,pageSize);
        //分页处理
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }


    @RequestMapping("/add")
    public String add(User user){
//        System.out.println(user);
        userService.add(user);
        return "list";
    }

    @RequestMapping("/updateByUser")
    public String updateByUser(User user){
        return "list";
    }
}
